package main

import (
	"github.com/micro/go-micro"
	"github.com/micro/go-micro/transport/grpc"
	"github.com/micro/go-micro/util/log"
	"github.com/micro/go-plugins/wrapper/select/roundrobin"
	"mall/admin/api/handler"
	rpc "mall/admin/api/proto"
	"time"

	rl "github.com/juju/ratelimit"
	"github.com/micro/go-plugins/wrapper/ratelimiter/ratelimit"
)


//func TraceBoot() {
//	apiURL := "http://192.168.0.111:9411/api/v1/spans"
//	hostPort,_ := os.Hostname()
//	serviceName := "go.micro.srv.order"
//
//	collector, err := zipkin.NewHTTPCollector(apiURL)
//	if err != nil {
//		log.Fatalf("unable to create Zipkin HTTP collector: %v", err)
//		return
//	}
//	tracer, err := zipkin.NewTracer(
//		zipkin.NewRecorder(collector, false, hostPort, serviceName),
//	)
//	if err != nil {
//		log.Fatalf("unable to create Zipkin tracer: %v", err)
//		return
//	}
//	opentracing.InitGlobalTracer(tracer)
//	return
//}



func main() {

	//TraceBoot()

	b := rl.NewBucketWithRate(float64(2), int64(2))
	wrapper := roundrobin.NewClientWrapper()
	// New Service
	service := micro.NewService(
		micro.Name("go.micro.srv.api"),
		micro.Version("latest"),
		micro.RegisterTTL(time.Second*10),
		micro.RegisterInterval(time.Microsecond*5),
		micro.Transport(grpc.NewTransport()),
		micro.WrapHandler(ratelimit.NewHandlerWrapper(b, false)),
		micro.WrapClient(wrapper),
		//micro.WrapHandler(wrapperTrace.NewHandlerWrapper()),
		//micro.WrapClient(wrapperTrace.NewClientWrapper()),

	)

	// Initialise service
	service.Init()

	// Register Handler
	rpc.RegisterRpcHandler(service.Server(), new(handler.Rpc))

	//// Register Struct as Subscriber
	//micro.RegisterSubscriber("go.micro.srv.api", service.Server(), new(subscriber.Rpc))
	//
	//// Register Function as Subscriber
	//micro.RegisterSubscriber("go.micro.srv.api", service.Server(), subscriber.Handler)

	// Run service
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
